namespace IAM.Interface.Common.Encrypt
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class EncryptDecrypt
    {
        private SymmetricAlgorithm mCryptoService = new RijndaelManaged();
        private string mKey = string.Empty;

        public EncryptDecrypt()
        {
            this.mCryptoService.Mode = CipherMode.CBC;
            this.Key = this.GenerateKey();
        }

        private string _dncrypt(string cryptoText)
        {
            if (cryptoText == null)
            {
                return null;
            }
            byte[] buffer = Convert.FromBase64String(cryptoText);
            byte[] legalKey = this.GetLegalKey();
            this.mCryptoService.Key = legalKey;
            this.SetLegalIV();
            ICryptoTransform transform = this.mCryptoService.CreateDecryptor();
            try
            {
                MemoryStream stream = new MemoryStream(buffer, 0, buffer.Length);
                CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
                StreamReader reader = new StreamReader(stream2);
                return reader.ReadToEnd();
            }
            catch
            {
                return null;
            }
        }

        private string _encrypt(string plainText)
        {
            byte[] inArray = null;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(plainText);
                byte[] legalKey = this.GetLegalKey();
                this.mCryptoService.Key = legalKey;
                this.SetLegalIV();
                ICryptoTransform transform = this.mCryptoService.CreateEncryptor();
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
                stream2.Write(bytes, 0, bytes.Length);
                stream2.FlushFinalBlock();
                stream2.Close();
                inArray = stream.ToArray();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                return null;
            }
            return Convert.ToBase64String(inArray, 0, inArray.GetLength(0));
        }

        public string Decrypt(string cryptoText)
        {
            return this._dncrypt(cryptoText);
        }

        public string Encrypt(string plainText)
        {
            return this._encrypt(plainText);
        }

        public string GenerateKey()
        {
            return this.genkey();
        }

        private string genkey()
        {
            char[] chArray = "6*(xIJv1CDE7^&F2rLOh0QRzSTWijklKPa#$%opGHw5g8y9!@UVstXYZqABu34)".ToCharArray();
            int length = chArray.Length;
            int index = 0;
            string str = "";
            for (int i = 0; i < 0x10; i++)
            {
                index = ((0x17 * i) + 0x25) % length;
                char ch = chArray[index];
                if ((i % 2) == 1)
                {
                    ch = (char) ((str[i - 1] + ch) + 12);
                }
                str = str + chArray[index];
            }
            return str;
        }

        public byte[] GetLegalKey()
        {
            if (this.mCryptoService.LegalKeySizes.Length > 0)
            {
                int num = this.mKey.Length * 8;
                int minSize = this.mCryptoService.LegalKeySizes[0].MinSize;
                int maxSize = this.mCryptoService.LegalKeySizes[0].MaxSize;
                int skipSize = this.mCryptoService.LegalKeySizes[0].SkipSize;
                if (num > maxSize)
                {
                    this.mKey = this.mKey.Substring(0, maxSize / 8);
                }
                else if (num < maxSize)
                {
                    int num5 = (num <= minSize) ? minSize : ((num - (num % skipSize)) + skipSize);
                    if (num < num5)
                    {
                        this.mKey = this.mKey.PadRight(num5 / 8, '*');
                    }
                }
            }
            PasswordDeriveBytes bytes = new PasswordDeriveBytes(this.mKey, Encoding.ASCII.GetBytes(string.Empty));
            return bytes.GetBytes(this.mKey.Length);
        }

        private void SetLegalIV()
        {
            this.mCryptoService.IV = new byte[] { 15, 0x6f, 0x13, 0x2e, 0x35, 0xc2, 0xcd, 0xf9, 5, 70, 0x9c, 0xea, 0xa8, 0x4b, 0x73, 0xcc };
        }

        public string Key
        {
            get
            {
                return this.mKey;
            }
            set
            {
                this.mKey = value;
            }
        }
    }
}

